package org.finesys.system.controller;

import java.util.List;

import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.constants.ValidationGroup;
import org.finesys.common.core.module.R;
import org.finesys.common.core.util.ValidationUtil;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.finesys.common.security.core.annotation.Inner;
import org.finesys.system.api.dto.ClientDetailsDto;
import org.finesys.system.api.entity.SysOauthClientDetails;
import org.finesys.system.constants.ControllerRestApiConfig;
import org.finesys.system.convert.SysOauthClientDetailsConvert;
import org.finesys.system.service.SysOauthClientDetailsService;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping(ControllerRestApiConfig.CLIENT)
@Tag(name = "客户端管理")
@RequiredArgsConstructor
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysOauthClientDetailsController {

    private final SysOauthClientDetailsService sysOauthClientDetailsService;


    /**
     * 添加
     */
    @Operation(summary = "添加")
    @LogAnnotation(title = "新增客户端", operateType = OperateType.INSERT)
    @PostMapping(value = RestApiConfig.ADD)
    @PreAuthorize("@pms.hasPermission('sys_client_add')")
    public R<Void> add(@RequestBody SysOauthClientDetails sysOauthClientDetails) {
        ValidationUtil.validateParam(sysOauthClientDetails, ValidationGroup.add.class);
        return R.judge(sysOauthClientDetailsService.save(sysOauthClientDetails));
    }

    /**
     * 修改
     */
    @Operation(summary = "修改")
    @LogAnnotation(title = "修改客户端", operateType = OperateType.UPDATE)
    @PutMapping(value = RestApiConfig.UPDATE)
    @PreAuthorize("@pms.hasPermission('sys_client_edit')")
    public R<Void> update(@RequestBody SysOauthClientDetails sysOauthClientDetails) {
        ValidationUtil.validateParam(sysOauthClientDetails, ValidationGroup.update.class);
        return R.judge(sysOauthClientDetailsService.updateClientDetailsById(sysOauthClientDetails));
    }

    /**
     * 删除
     */
    @Operation(summary = "删除")
    @LogAnnotation(title = "删除客户端", operateType = OperateType.DELETE)
    @DeleteMapping(value = RestApiConfig.DELETE)
    @PreAuthorize("@pms.hasPermission('sys_client_del')")
    public R<Void> delete(@RequestBody Long[] ids) {
        return R.judge(sysOauthClientDetailsService.removeClientDetailsByIds(ids));
    }

    /**
     * 获取单条
     */
    @Operation(summary = "获取单条")
    @GetMapping(value = RestApiConfig.GET + "/{id}")
    public R<ClientDetailsDto> findById(@PathVariable Long id) {
        return R.success(SysOauthClientDetailsConvert.INSTANCE.convert(sysOauthClientDetailsService.getById(id)));
    }


    /**
     * 获取单条
     */
    @Inner
    @Operation(summary = "获取单条")
    @GetMapping(value = "/getByClientId/{clientId}")
    public R<ClientDetailsDto> getByClientId(@PathVariable(value = "clientId") String clientId) {
        SysOauthClientDetails sysOauthClientDetails = sysOauthClientDetailsService.getOne(Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId), false);
        return R.success(SysOauthClientDetailsConvert.INSTANCE.convert(sysOauthClientDetails));
    }


    /**
     * 获取全部
     */
    @Operation(summary = "获取全部")
    @GetMapping(value = RestApiConfig.ALL)
    public R<List<ClientDetailsDto>> findAll() {
        List<SysOauthClientDetails> list = sysOauthClientDetailsService.findAll();
        return R.success(SysOauthClientDetailsConvert.INSTANCE.convert(list));
    }

    /**
     * 简单分页查询
     *
     * @param page                  分页对象
     * @param sysOauthClientDetails 系统终端
     * @return
     */
    @GetMapping(value = RestApiConfig.PAGE)
    public R<IPage<ClientDetailsDto>> getPage(Page<SysOauthClientDetails> page,
                                              SysOauthClientDetails sysOauthClientDetails) {
        return R.success(SysOauthClientDetailsConvert.INSTANCE.convert(sysOauthClientDetailsService.findPage(page, sysOauthClientDetails)));
    }

    /**
     * 同步客户端缓存
     */
    @LogAnnotation(title = "同步客户端缓存", operateType = OperateType.CLEAN)
    @PutMapping(RestApiConfig.SYNC)
    @PreAuthorize("@pms.hasPermission('sys_client_del')")
    public R<?> sync() {
        sysOauthClientDetailsService.sync();
        return R.success();
    }
}
